local items = {
	settingsfile = "FireMage.json",
	callback = GUICallback,
	{ type = "title", text = "火法 |c0000CED1PvE" },
	{ type = "separator" },
	{ type = "title", text = "|cFF00FF00输出循环|r" },
	{ type = "separator" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(55360))..":26:26\124t  活体炸弹 <", tooltip = "活体炸弹", enabled = true, key = "LivingBomb"},
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42873))..":26:26\124t  火焰冲击", tooltip = "火焰冲击", enabled = true, key = "Fireblast" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42859))..":26:26\124t  灼烧", tooltip = " 灼烧", enabled = true, key = "Scorch" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42891))..":26:26\124t  炎爆术", tooltip = "炎爆术", enabled = true, key = "Pyroblast" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42833))..":26:26\124t  火球术", tooltip = "火球术", enabled = true, key = "Fireball" },
	{ type = "separator" },
	{ type = "title", text = "|cffFFFF00AOE功能" },
	{ type = "separator" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42926))..":26:26\124t 智能五连 怪物数量 >", tooltip = "数量不包括目标", enabled = true, value = 2, key = "FiveContinuous" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(55360))..":26:26\124t 使用 活体炸弹AOE", tooltip = "活体炸弹AOE", enabled = true, key = "AOELivingBomb" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42926))..":26:26\124t 使用 烈焰风暴 怪物数量 >", tooltip = "数量不包括目标 8秒检测一次", enabled = true, value = 2, key = "Flamestrike" },
	{ type = "separator" },
    { type = "page", number = 1, text = "|cffFFFF00保命功能|r" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(55342))..":26:26\124t  镜像", tooltip = "镜像消仇恨", enabled = true, key = "MirrorImage" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(45438))..":26:26\124t  寒冰屏障 HP% <", tooltip = "唤醒 < HP%", enabled = true, value = 50, key = "IceBlock" },
	{ type = "entry", text = "\124T"..GetItemIcon(36894)..":26:26\124t 使用 治疗石 HP% <", tooltip = "治疗石", enabled = true, value = 60, key = "FelHealthstone" },
	{ type = "page", number = 2, text = "|cFF00FF00辅助功能|r" },
	{ type = "separator" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(2139))..":26:26\124t  法术反制", tooltip = "法术反制", enabled = true, key = "Counterspell" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(30449))..":26:26\124t  法术吸取", tooltip = "法术吸取", enabled = true, key = "Spellsteal" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(475))..":26:26\124t  驱散诅咒", tooltip = "驱散诅咒", enabled = true, key = "RemoveCurse" },
	{ type = "entry", text = "\124T"..GetItemIcon(36799)..":26:26\124t  法力宝石 MP% <", tooltip = "使用法力宝石", enabled = true, value = 70, key = "ManaGem" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(11129))..":26:26\124t  燃烧BUFF", tooltip = "燃烧BUFF增加法伤", enabled = true, key = "Combustion" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(12051))..":26:26\124t  唤醒 MP% <", tooltip = "唤醒 < MP%", enabled = true, value = 10, key = "Evocation" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(43046))..":26:26\124t  熔岩护甲", tooltip = "熔岩护甲", enabled = true,  key = "MoltenArmor" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(42995))..":26:26\124t  奥术智慧", tooltip = "奥术智慧", enabled = true,  key = "Arcane Brilliance" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(33717))..":26:26\124t  制作宝石面包", tooltip = "制作宝石面包", enabled = true, key = "ConjureManaGem" },
};
 
local function GetSetting(name)
    for k, v in ipairs(items) do
        if v.type == "entry"
         and v.key ~= nil
         and v.key == name then
            return v.value, v.enabled
        end
    end
end;

local function OnLoad()
	ni.GUI.AddFrame("FireMage", items);
end;

local function OnUnLoad()  
	ni.GUI.DestroyFrame("FireMage");
end;

local lyfb = 42926;

local spells = {
	IceBlock = {id = 45438, name = GetSpellInfo(45438)},
	ShockWave = {id = 42945, name = GetSpellInfo(42945)},
	DragonBreathingTechnique = {id = 42950, name = GetSpellInfo(42950)},
	FelHealthstone = {id = 36894, name = GetItemInfo(36894)},
	ConjureManaGem = {id = 42985, name = GetSpellInfo(42985)},
	MoltenArmor = {id = 43046, name = GetSpellInfo(43046)},
	ArcaneBrilliance = {id = 43002, name = GetSpellInfo(43002)},
	ArcaneIntellect = {id = 42995, name = GetSpellInfo(42995)},
	Evocation = {id = 12051, name = GetSpellInfo(12051)},
	Pyroblast = {id = 42891, name = GetSpellInfo(42891)},
	Scorch = {id = 42859, name = GetSpellInfo(42859)},
	LivingBomb = {id = 55360, name = GetSpellInfo(55360)},
	MirrorImage = {id = 55342, name = GetSpellInfo(55342)},
	Combustion = {id = 11129, name = GetSpellInfo(11129)},
	Fireblast = {id = 42873, name = GetSpellInfo(42873)},
	Fireball = {id = 42833, name = GetSpellInfo(42833)},
	Flamestrike = {id = 42926, name = GetSpellInfo(42926)},
	Culinaryart = {id = 42956, name = GetSpellInfo(42956)},
	RemoveCurse = {id = 475, name = GetSpellInfo(475)},
	Spellsteal = {id = 30449, name = GetSpellInfo(30449)},
	Counterspell = {id = 2139, name = GetSpellInfo(2139)},
};

local Cache = {
		enemies = ni.player.enemiesinrange(35),
};

local function ValidUsable(id, tar)
	if ni.spell.available(id) and ni.spell.valid(tar, id, true, true) then
		return true
	end
	return false
 end;

 local function NValidUsable(id, tar)
	if ni.spell.available(id) and ni.spell.valid(tar, id, false, true, true) then
		return true
	end
	return false
 end;

 local function Ddmb(tar)
   if ni.unit.iscasting(tar) or ni.unit.ischanneling(tar) then
        return true end
	return false
 end;

local queue = {
	"Cache",
	"Pause",
	"Ice Block",
	"Spellsteal",
	"Counterspell",
	"RemoveCurse",
	"MoltenArmor",
	"Arcane Brilliance",
	"FelHealthstone",
	"MirrorImage",
	"ManaGem",
	"Evocation",
	"Pyroblast",
	"FiveContinuous",
	"AOELivingBomb",
	"Flamestrike",
	"Fireblast",
	"Scorch",
	"Combustion",
	"LivingBomb",
	"Fireball",
	"ConjureManaGem",
    
};
local delaytime = 0
local abilities = {
	["Cache"] = function()
			Cache.enemies = ni.player.enemiesinrange(35)
			FfsshifapandunaF = FfsshifapandunaF or CreateFrame("frame","FfsshifapandunaF")      
            FfsshifapandunaF:RegisterEvent("UNIT_SPELLCAST_SENT")     
            FfsshifapandunaF:SetScript("OnEvent", function(_,_,a,b,_,c)     
            if a=="player" and b=="烈焰风暴"  then
			   aktm = GetTime() 
			   if GetTime()  -  aktm > 5 then lyfb = 42926  end
			   if lyfb == 42925 then  lyfb = 42926
			   else lyfb = 42925
			   end 
            FfsshifapandunaF:UnregisterEvent("UNIT_SPELLCAST_SUCCEEDED")  end  end) 
		end,

	["Pause"] = function()
		if UnitAffectingCombat("player") then
		    if  not UnitCanAttack("player", "target")  then
			ni.player.runtext("/startattack")
			return true
			end
		end
		if IsMounted()
			or UnitIsDeadOrGhost("player")
			or ni.player.iscasting()
			or ni.player.ischanneling() then
			return true
		end
	end,

	["Ice Block"] = function()
	local value, enabled = GetSetting("IceBlock")
		if enabled then
		   if UnitAffectingCombat("player")
		    and ni.player.hp() <= value
			and ni.spell.available(spells.IceBlock.id)
		   then
				ni.spell.cast(spells.IceBlock.name)	return true;
		   end
		end 
	end,

	["Remove Curse"] = function()	--驱散诅咒
	local _, enabled = GetSetting("RemoveCurse")
        if enabled  then
           for i = 1, #ni.members do
               local ally = ni.members[i].unit;
               if ni.unit.debufftype( ally, "Curse") 
                  and NValidUsable(spells.RemoveCurse.id, ally)
			   then
                 ni.spell.cast(spells.RemoveCurse.name, ally) return true;
               end
		   end
        end
    end,

    ["Spellsteal"] = function()     --偷取
	local _, enabled = GetSetting("Spellsteal")
		if enabled  then
			for i = 1, #Cache.enemies do
				local target = Cache.enemies[i].guid
				local x, y, z, facing, unittype, targett, height = ni.unit.info(target)
				if  ni.unit.exists(targett) 
				    and ni.unit.bufftype(target, "Magic")
					and ValidUsable(spells.Spellsteal.id,target)
				then
					ni.spell.cast(spells.Spellsteal.name, target)	return true
				end
			end
		end
	end,

	["Counterspell"] = function()   --反制
    local _, enabled = GetSetting("Counterspell")
		if enabled  then
			for i = 1, #Cache.enemies do
				local target = Cache.enemies[i].guid
				local x, y, z, facing, unittype, targett, height = ni.unit.info(target)
				if  ni.unit.exists(targett) 
						and Ddmb(target)
						and ValidUsable(spells.Counterspell.id,target)
				then
					ni.spell.cast(spells.Counterspell.name, target)	return true
				end
			end
		end
	end,

	["FelHealthstone"] = function()   --治疗石
	local value, enabled = GetSetting("FelHealthstone")
		if enabled then
		   if UnitAffectingCombat("player")
			  and ni.player.hp() <= value
			  and ni.player.hasitem(spells.FelHealthstone.name) 
			  and ni.player.itemcd(spells.FelHealthstone.name) < 1 
		   then 
				ni.player.useitem(spells.FelHealthstone.name) return true;
		   end
		end
	end,

	["MoltenArmor"] = function()  --熔岩护甲
	local _, enabled = GetSetting("MoltenArmor")
	   if enabled then
		 if ni.spell.available(spells.MoltenArmor.id)
			and not ni.player.buff(spells.MoltenArmor.id)
	     then
				ni.spell.cast(spells.MoltenArmor.name)	return true;
         end
	   end
	end,

	["Arcane Brilliance"] = function()   --奥术智慧
	local _, enabled = GetSetting("Arcane Brilliance")
	   if enabled then
		 if NValidUsable(spells.ArcaneIntellect.id,"player") 
		    and not ni.player.buffs("42995||43002") then
		    if ni.player.hasitem(17020) then
				ni.spell.cast(spells.ArcaneBrilliance.name,"player") return true;
			else ni.spell.cast(spells.ArcaneIntellect.name,"player") return true;
		    end
		 end
       end
	end, 

	["ConjureManaGem"] = function()
	local _, enabled = GetSetting("ConjureManaGem")
	   if enabled then
	     if not UnitAffectingCombat("player") 
		    and not UnitCanAttack("player", "target")
			and not ni.player.ismoving()
		 then
		      if not ni.player.hasitem(33312)    --宝石
		         and ni.spell.available(spells.ConjureManaGem.id)
		      then 
				 ni.spell.cast(spells.ConjureManaGem.name) return true;	
		      end 
		      if not ni.player.hasitem(43523)    --造餐
		         and ni.spell.available(spells.Culinaryart.id)
		      then 
				 ni.spell.cast(spells.Culinaryart.name) return true;	
		      end 
		 end
	   end
	end,

	["ManaGem"] = function()   --使用宝石
	local value, enabled = GetSetting("ManaGem")
		if enabled then
		    if ni.player.power() <= value
			   and ni.player.hasitem(33312) 
			   and ni.player.itemcd(33312) < 1 
			then 
				ni.player.useitem(33312) return true;
			end
		end
	end,
	
	["Evocation"] = function()   --唤醒
	local value, enabled = GetSetting("Evocation")
		if enabled  then
		   if ni.spell.available(spells.Evocation.id)
			  and ni.player.power() <= value 
		   then
				ni.spell.cast(spells.Evocation.name) return true;
		   end
		end 
	end,

	["Pyroblast"] = function()  --瞬发
	local _, enabled = GetSetting("Pyroblast")
	  if enabled then
	    if ni.player.buff(54741, "player") 
		   and ni.spell.available(spells.Flamestrike.id)
		then
		    ni.spell.castat(lyfb,"target")  return true;
	    end
		if ni.player.buff(48108, "player") 
		   and ValidUsable(spells.Pyroblast.id, "target")
		then 
			ni.spell.cast(spells.Pyroblast.name, "target") return true;
		end
	  end
	end,

	["FiveContinuous"] = function()  --五连
	local value, enabled = GetSetting("FiveContinuous")
	local enear = #ni.player.enemiesinrange(7) --8码内敌人数量 --
	   if enabled then
	     if enear >= value 
		    and not ni.player.buffs("54741||48108")
		 then
		    if ni.spell.available(spells.ShockWave.id) 
			then
		       ni.spell.cast(spells.ShockWave.name)  return true;
		    end
		    if ni.spell.available(spells.DragonBreathingTechnique.id) 
			then
		       ni.spell.cast(spells.DragonBreathingTechnique.name) return true;	
		    end
	     end
	   end
	end,

	["Scorch"] = function()  --灼烧
	local _, enabled = GetSetting("Scorch")
		if enabled then 
		   if not ni.unit.debuffs("target", "22959 || 17803") 
		     and ValidUsable(spells.Scorch.id, "target")
		     and not ni.player.ismoving() 
		   then
				ni.spell.cast(spells.Scorch.name, "target")	return true;
		   end
		end
	end,

	["AOELivingBomb"] = function()  --自动活体炸弹
	local _, enabled = GetSetting("AOELivingBomb")
        if enabled  then
			for i = 1, #Cache.enemies do
				local target = Cache.enemies[i].guid
				local x, y, z, facing, unittype, targett, height = ni.unit.info(target)
				if  ni.unit.exists(targett)
					and ValidUsable(spells.LivingBomb.id,target)
					and not ni.unit.debuff(target, 55360, "player")
				then
					ni.spell.cast(spells.LivingBomb.name, target) return true
				end
			end
		end
	end,

	["LivingBomb"] = function()  --活体炸弹
	local _, enabled = GetSetting("LivingBomb")
	    if enabled  then
		   if ValidUsable(spells.LivingBomb.id,"target")
			  and not ni.unit.debuff("target", spells.LivingBomb.id, "player")
		   then
				ni.spell.cast(spells.LivingBomb.name,"target") return true;
		   end
		end
	end,


	["MirrorImage"] = function()  --镜像消仇恨
	local _, enabled = GetSetting("MirrorImage")
		if enabled then
		   if ni.player.threat() >= 2
		      and ni.spell.available(spells.MirrorImage.id) 
		   then
			  ni.spell.cast(spells.MirrorImage.name, "player")	return true;
		   end
		end
	end,

	["Combustion"] = function()   --燃烧
	local _, enabled = GetSetting("Combustion")
		if enabled then
		   if ni.spell.available(spells.Combustion.id)  
		   then
				ni.spell.cast(spells.Combustion.name, "player")	return true;
		   end
		end
	end,

	["Fireblast"] = function()   --火冲
	local _, enabled = GetSetting("Fireblast")
	    if enabled then
	       if ValidUsable(spells.Fireblast.id,"target") 
			  and ni.player.ismoving() 
		   then
				ni.spell.cast(spells.Fireblast.name, "target")	return true;
		   end
		end
	end,

	["Fireball"] = function()    --火球
	local _, enabled = GetSetting("Fireblast")
	    if enabled then
	       if ValidUsable(spells.Fireball.id, "target") 
			  and not ni.player.ismoving() 
		   then
				ni.spell.cast(spells.Fireball.name, "target") return true;
		   end
		end
	end,
	
	["Flamestrike"] = function()   --自动烈焰风暴
	local value, enabled = GetSetting("Flamestrike")
		if enabled then
			for i = 1, #Cache.enemies do
				local target = Cache.enemies[i].guid
				local x, y, z, facing, unittype, targett, height = ni.unit.info(target)
				local enear = #ni.unit.enemiesinrange(target, 15)
				if  ni.unit.exists(targett) 
				    and enear >= value
					and not ni.player.ismoving() 
				    and ValidUsable(spells.Fireball.id, target)
				then
				    ni.spell.castat(lyfb,target)	return true
				end
			end
		end
	end,



};
ni.bootstrap.profile("FireMage", queue, abilities, OnLoad, OnUnLoad);	
